package com.atguigu.srb.base.utils;

import com.atguigu.srb.base.config.MinioProperties;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Component
public class MinioClientUtil {
	
	@Autowired
	private MinioProperties properties;
 
	private MinioClient client;
	
    //初始化minio client
	private void init() throws NoSuchAlgorithmException, InvalidKeyException, IOException,
			InvalidResponseException, InsufficientDataException, ServerException, ErrorResponseException,
			XmlParserException, InternalException {
		if (null != client) {
			return;
		}
 
		client = MinioClient.builder()
				.endpoint(properties.getUrl())
				.credentials(properties.getAccessKey(), properties.getSecretKey())
				.build();
 
		makeBucket(properties.getBucket());
	}
	
    //创建自定义的存储桶
    //这里只做了简单的桶存在判断，这里还可以添加设置桶的策略等
	public void makeBucket(String bucket) throws IOException, InvalidKeyException, InvalidResponseException,
			InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
			ErrorResponseException {
		boolean bucketExist = client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
		if (!bucketExist) {
			client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
		}
	}

	/**
	 * @return void
	 * @Description
	 * @Date 11:58 2022/8/19
	 * @Param [bucket=同名称, fileName=文件路径即名称, ins=上传的输入流, fileSize=上传的文件大小]
	 **/
	public void uploadFile(String bucket, String fileName, InputStream ins, long fileSize)
			throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
			NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
			ErrorResponseException {

		init();
		makeBucket(bucket);

		PutObjectArgs.Builder putObjectArgsBuilder = PutObjectArgs.builder()
				.bucket(bucket)
				.object(fileName)
				.stream(ins, fileSize, 5 * 1024 * 1024);

		client.putObject(putObjectArgsBuilder.build());
	}

	/**
	 * @return java.io.InputStream
	 * @Description
	 * @Date 12:09 2022/8/19
	 * @Param [bucket=存储桶名称, fileName=文件路径即名称]
	 **/
	public InputStream download(String bucket, String fileName)
			throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
			NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
			ErrorResponseException {

		init();

		GetObjectArgs.Builder getObjectArgsBuilder = GetObjectArgs.builder()
				.bucket(bucket)
				.object(fileName);

		return client.getObject(getObjectArgsBuilder.build());
	}

	/**
	 * @return java.lang.String
	 * @Description
	 * @Date 12:14 2022/8/19
	 * @Param [bucket=存储桶名称, fileName=文件路径即名称]
	 **/
	private String getMinioURL(String bucket, String fileName) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, ServerException, XmlParserException, InternalException, InsufficientDataException, ErrorResponseException {
		if (null == client) {
			init();
		}

		GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder()
				.method(Method.GET)
				.bucket(bucket)
				.object(fileName)
				.expiry(60 * 60 * 24)   //生成的预签名url可访问的有效时间，最大期限7天
				.build();
		return client.getPresignedObjectUrl(build);
	}

	/**
	 * @return void
	 * @Description
	 * @Date 14:19 2022/8/19
	 * @Param [bucket=存储桶名称, fileName=文件路径即名称]
	 **/
	public void delete(String bucket, String fileName) throws IOException, InvalidKeyException, InvalidResponseException,
			InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException,
			ErrorResponseException {
		init();
		client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(fileName).build());
	}
	
}